﻿using MySqlConnector;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Practice_DotNet.Helper
{
    internal static class SQLHelper
    {
        public static string connectionString { get; set; } = "Server=localhost;Port=3306;Database=localhosttestdb;User ID=root;Password=;SslMode=None;";
        public static async Task<int> SQL_CRUDWithoutRead(string sql)
        {
            using (MySqlConnection connection = new(connectionString))
            {
                await connection.OpenAsync();
                using (MySqlCommand cmd = new MySqlCommand(sql, connection))
                {
                    var effectLines = await cmd.ExecuteNonQueryAsync();
                    if (effectLines <= 0)
                    {
                        return 0;
                    }
                    return effectLines;
                }
            }
        }
        public static async Task<DataTable> SQL_CRUDOnlyRead(string sql)
        {
            using (MySqlConnection connect = new(connectionString))
            {
                await connect.OpenAsync();
                using (MySqlCommand cmd = new(sql, connect))
                {
                    MySqlDataAdapter adapter = new(cmd);
                    DataSet ds = new();
                    int linesCount = adapter.Fill(ds);
                    return ds.Tables[0];
                }
            }
        }
    }
}
